home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 04 / 1 / DISK0416.ZIP / ROFF47.C < prev    next >
C/C++ Source or Header  |  1985-08-08  |  15KB  |  503 lines

  1. /********************************************************/
  2. /*                                                      */
  3. /*                  ROFF4, Version 1.60                 */
  4. /*                                                      */
  5. /*(C) 1983,4 by Ernest E. Bergmann                      */
  6. /*              Physics, Building #16                   */
  7. /*              Lehigh Univerisity                      */
  8. /*              Bethlehem, Pa. 18015                    */
  9. /*                                                      */
  10. /* Permission is hereby granted for all commercial and  */
  11. /* non-commercial reproduction and distribution of this */
  12. /* material provided this notice is included.           */
  13. /*                                                      */
  14. /********************************************************/
  15. /*Feb 15, 1984*/
  16. /*January 9, 1984*/
  17. #include "roff4.h"
  18. /*assuming REVSCROLL is FALSE*/
  19. /*output OUTBUF2 whith the vertical height of the mainline
  20. specified by VLINENO,FVLINENO[they must not be changed here].
  21. Excessive superscripting will be pushed down.*/
  22.  
  23. printout()
  24. {
  25.       int level,top,bot;     /*"up" is negative;units fractional*/
  26.       int lsave,fsave;
  27.  
  28.       OUTBUF2[BPOS]='\0';
  29.       fsave=FVLINENO;
  30.       lsave=VLINENO;
  31.       level=FRVAL*(PLINENO-VLINENO)+FPLINENO-FVLINENO;
  32.       if(!OLDBOT) level++;
  33.       excurs(&OUTBUF2[0],&top,&bot);
  34.       if(top>level) level=top;
  35.  
  36.       if(!REVSCROLL) FVLINENO += level;
  37.       padv();
  38.       for(;level<=bot;level++)
  39.       {
  40.             OCNT=0;
  41.             do {
  42.                   OCNT++; 
  43.                   flp(level,FALSE); 
  44.             }
  45.             while(retype());
  46.             if(level<bot)
  47.             {
  48.                   fraction();
  49.                   putchar('\r');
  50.                   putchar('\n');
  51.                   FPLINENO++;
  52.             }
  53.       }
  54.       /* flp(level,TRUE); */  /*update UF, XF, MCNT*/
  55.       updatef();
  56.       OUTBUF2[0]=BPOS=0;
  57.       OLDLN=VLINENO=lsave;
  58.       OLDBOT=bot;
  59.       FVLINENO=fsave;
  60. }
  61. /****************************************/
  62. /*moves printer vertically so that its position is specified
  63. by VLINENO,FVLINENO*/
  64. padv()
  65. {
  66.       int w,f;       /*whole,fractional lines*/
  67.       w=VLINENO-PLINENO;
  68.       f=FVLINENO-FPLINENO;
  69.       while(f<0) {
  70.             w--; 
  71.             f += FRVAL; 
  72.       }
  73.       while(f>=FRVAL) {
  74.             w++; 
  75.             f -= FRVAL; 
  76.       }
  77.       if(w<0){
  78.             fprintf(STDERR,"padv():VL=%d,PL=%d\n",
  79.             VLINENO,PLINENO);
  80.             if(REVSCROLL) backup(w*FRVAL+f);
  81.             else {
  82.                   VLINENO +=w;
  83.                   FVLINENO +=f;
  84.                   while(FVLINENO<0){
  85.                         VLINENO++;
  86.                         FVLINENO+=FRVAL;
  87.                   }
  88.                   while(FVLINENO<FRVAL){
  89.                         VLINENO--;
  90.                         FVLINENO-=FRVAL;
  91.                   }
  92.                   return;
  93.             }
  94.       }
  95.       if(FRQ) {
  96.             while(f--){
  97.                   putchar('\r');
  98.                   putchar('\n'); 
  99.                   FPLINENO++; 
  100.             }
  101.             if(w){
  102.                   whole();
  103.                   while(w--){
  104.                         putchar('\r');
  105.                         putchar('\n');
  106.                         PLINENO++;
  107.                   }
  108.             }
  109.       }
  110.       else    {
  111.             while(w--){
  112.                   putchar('\r');
  113.                   putchar('\n'); 
  114.                   PLINENO++; 
  115.             }
  116.             if(f){
  117.                   fraction();
  118.                   while(f--){
  119.                         putchar('\r');
  120.                         putchar('\n');
  121.                         FPLINENO++;
  122.                   }
  123.             }
  124.       }
  125.       while(FPLINENO>=FRVAL) {
  126.             PLINENO++; 
  127.             FPLINENO -= FRVAL; 
  128.       }
  129. }
  130. /****************************************/
  131. backup(i)       /*not yet implemented*/
  132. int i;          /*# of fractional lines(probably negative)*/
  133. {
  134.       fprintf(STDERR,"\nCan't back up yet\n");
  135. }
  136. /**************************************************/
  137. excurs(str,t,b) /*finds the topmost and bottommost line
  138.                 positions of str*/
  139. char *str;
  140. int *t,*b;
  141. {
  142.       int l;
  143.       char c;
  144.       *t=*b=l=0; /*current line position */
  145.       c=*str;
  146.       while(c){
  147.             if(c==CFVAL){
  148.                   if(c=*(++str))
  149.                         switch(c)
  150.                         {
  151.                         case '+':
  152.                               l--; 
  153.                               if(l<*t) *t=l;
  154.                               c=*(++str); 
  155.                               break;
  156.                         case '-':
  157.                               l++; 
  158.                               if(l>*b) *b=l;
  159.                               c=*(++str); 
  160.                               break;
  161.                         default : 
  162.                               c=*(++str); 
  163.                               break;
  164.                         }
  165.             }
  166.             else c=*(++str);
  167.       }
  168. }
  169.  
  170. /**************************************************/
  171. flp(level,updat)/*fancy line print at a given vertical level
  172.                 the string in OUTBUF2[] with backspacing,
  173.                 underlining, and strikout.  To permit boldface
  174.                 it modifies DBUF[],DPOS so that retype can be
  175.                 used to patch up OUTBUF2 for resubmittal to
  176.                 flp()*/
  177. int level;      /* current vertical level to print*/
  178. int updat;      /* boolean for update of UF,XF,MCNT*/
  179. {
  180.       int i;
  181.       BLKCNT=lbc(level,OUTBUF2);
  182.       FIRST=TRUE;
  183.       while((BLKCNT>0)||updat)
  184.       {
  185.             prpass(level,updat); 
  186.             putchar('\r');
  187.             updat=FIRST=FALSE;
  188.       }
  189.       if(XCOL>-1){
  190.             for(i=0;i<=XCOL;i++)
  191.                   putchar(XBUF[i]);
  192.             putchar('\r');
  193.       }
  194.       if(UCOL>-1){
  195.             for(i=0;i<=UCOL;i++)
  196.                   putchar(UBUF[i]);
  197.             putchar('\r');
  198.       }
  199.       if((UCOL>-1)||(XCOL>-1)) initxu();
  200. }
  201.  
  202. /**************************************************/
  203. retype()        /*restores characters into OUTBUF2 from DBUF
  204.                 that need to be overstruck again*/
  205. {
  206.       int i;
  207.       if(DPOS==-1) return(FALSE);
  208.       else    {
  209.             for(i=0;i<=DPOS;i++)
  210.             {
  211.                   if(DBUF[i])
  212.                   {
  213.                         OUTBUF2[i]=DBUF[i];
  214.                         DBUF[i]=FALSE;
  215.                   }
  216.             }
  217.             DPOS=-1;
  218.             return(TRUE);
  219.       }
  220. }
  221.  
  222. /**************************************************/
  223. int lbc(lev,str) /*counts printable chars in line level and
  224.                 above; parity must be reset*/
  225. int lev; /*=0 main line,=-1 superscripts,=+1 subscripts, etc.*/
  226. char *str;
  227. {
  228.       char c;
  229.       int l,n;
  230.       l=n=0;
  231.       c=*str;
  232.       while(c){
  233.             if(c==CFVAL){
  234.                   if(c=*(++str))
  235.                         switch(c)
  236.                         {
  237.                         case '+':
  238.                               l--;
  239.                               c=*(++str);
  240.                               break;
  241.                         case '-':
  242.                               l++;
  243.                               c=*(++str);
  244.                               break;
  245.                         default: 
  246.                               c=*(++str);
  247.                               break;
  248.                         }   
  249.             }
  250.             else    {
  251.                   if((c>' ')&&(l<=lev)) if(c!=TCVAL) n++;
  252.                   c=*(++str);
  253.             }
  254.       }
  255.       return(n);
  256. }
  257.  
  258. /**************************************************/
  259. prpass(lev,updat) /*printer pass initial cr; no lf anywhere*/
  260. int lev; /*=0 main line,=-1 superscripts,=+1 subscripts, etc.*/
  261. int updat;/*boolean to update UF,XF,MCNT*/
  262. {
  263.       char ch;
  264.       int l;
  265.       int xfs,ufs,mcnts;      /*save variables*/
  266.       int p1,p2,p3;           /*position holders*/
  267.       int cp2;        /*for tabulation calculation*/
  268.       xfs=XF; 
  269.       ufs=UF; 
  270.       mcnts=MCNT;
  271.       p1=p2=p3=l=BPOS=CP=PP=0;
  272.       while(ch=OUTBUF2[BPOS])
  273.       {
  274.             switch (class(ch))
  275.             {
  276.             case   BLACK:/*print it if posssible*/
  277.                   if((PP>CP)||(l>lev)){
  278.                         CP++;
  279.                         BPOS++;
  280.                         break;
  281.                   }
  282.                   else    {
  283.                         while(CP>PP){
  284.                               putchar(' ');
  285.                               PP++;
  286.                         }
  287.                         if(ch==SCVAL)putchar(' ');
  288.                         else putchar(ch);
  289.                         PP++;
  290.                         if(MCNT>OCNT)
  291.                         {
  292.                               DBUF[BPOS]=OUTBUF2[BPOS];
  293.                               if(BPOS>DPOS) DPOS=BPOS;
  294.                         }
  295.                         OUTBUF2[BPOS++]=' ';
  296.                         if(UF&&FIRST)UBUF[UCOL=CP]=UCHAR;
  297.                         if(XF&&FIRST)XBUF[XCOL=CP]=XCHAR;
  298.                         BLKCNT--; 
  299.                         CP++;
  300.                   } 
  301.                   break;
  302.             case    WHITE:/*assume blank*/
  303.                   CP++;
  304.                   BPOS++;
  305.                   break;
  306.             case    TRANSLATE:/*similar to BLACK and WHITE*/
  307.                   ch=OUTBUF2[++BPOS];
  308.                   if((PP>CP)||(l>lev)||(ch==' '))
  309.                   {
  310.                         CP++;
  311.                         BPOS++;
  312.                         break;
  313.                   }
  314.                   else
  315.                       {
  316.                         while(CP>PP){
  317.                               putchar(' ');
  318.                               PP++;
  319.                         }
  320.                         trch(ch);
  321.                         PP++;
  322.                         if(MCNT>OCNT)
  323.                         {
  324.                               DBUF[BPOS]=OUTBUF2[BPOS];
  325.                               DBUF[BPOS-1]=OUTBUF2[BPOS-1];
  326.                               if(BPOS>DPOS) DPOS=BPOS;
  327.                         }
  328.                         OUTBUF2[BPOS++]=' ';
  329.                         if(UF&&FIRST)UBUF[UCOL=CP]=UCHAR;
  330.                         if(XF&&FIRST)XBUF[XCOL=CP]=XCHAR;
  331.                         BLKCNT--; 
  332.                         CP++;
  333.                   } 
  334.                   break;
  335.             case    CONTROL:/*decode on following letter*/
  336.                   ch=OUTBUF2[++BPOS];
  337.                   if(CPTR[ch-' ']) {
  338.                         while (CP>PP) {   /* 2 Mar,85         */
  339.                               putchar(' ');   /* update PP before */
  340.                               PP++;           /* printing printer */
  341.                         }               /* control string   */
  342.                         pcont(ch);
  343.                   }
  344.                   else switch(ch)
  345.                   {
  346.                   case 'h':
  347.                   case 'H':/*backspace*/
  348.                         if(CP)CP--;
  349.                         break;
  350.                   case '+': 
  351.                         l--; 
  352.                         break;
  353.                   case '-': 
  354.                         l++; 
  355.                         break;
  356.                   case 'U': 
  357.                         UF=TRUE;
  358.                         break;
  359.                   case 'u': 
  360.                         UF=FALSE;
  361.                         break;
  362.                   case 'X': 
  363.                         XF=TRUE;
  364.                         break;
  365.                   case 'x': 
  366.                         XF=FALSE;
  367.                         break;
  368.                   case 'B': 
  369.                         MCNT *=3;
  370.                         break;
  371.                   case 'b': 
  372.                         if(!(MCNT /=3))MCNT=1;
  373.                         break;
  374.                   case 'D': 
  375.                         MCNT *=2;
  376.                         break;
  377.                   case 'd': 
  378.                         if(!(MCNT /=2))MCNT=1;
  379.                         break;
  380.                   case '(': 
  381.                         p1=CP;
  382.                         break;
  383.                   case ')': 
  384.                         CP=p1;
  385.                         break;
  386.                   case '[': 
  387.                         p2=CP;
  388.                         break;
  389.                   case ']': 
  390.                         CP=p2;
  391.                         break;
  392.                   case '{': 
  393.                         p3=CP;
  394.                         break;
  395.                   case '}': 
  396.                         CP=p3;
  397.                         break;
  398.                   default:/*?,ignore*/
  399.                         ;
  400.                         break;
  401.                   } 
  402.                   BPOS++; 
  403.                   break;
  404.             case    SENTINEL: 
  405.                   OUTBUF2[BPOS]=0;
  406.                   break;
  407.             case    HTAB: 
  408.                   for(cp2=0;CP>=0;cp2+=TABSIZ)CP-=TABSIZ;
  409.                   CP=cp2; 
  410.                   BPOS++; 
  411.                   break;
  412.             case    OTHERS:
  413.                   fprintf(STDERR,"\nweird character value[octal]: %o\n",ch);
  414.                   BPOS++;
  415.                   break;
  416.             }
  417.       }
  418.       if(!updat){/*restore original values*/
  419.             NEWXF=XF;
  420.             XF=xfs;
  421.             NEWUF=UF;
  422.             UF=ufs;
  423.             NEWMCNT=MCNT;
  424.             MCNT=mcnts;
  425.       }
  426. }
  427. /**************************************************/
  428. updatef()    /* 3rd Mar, 85         */
  429. {            /* By Conrad Kwok      */
  430.       XF=NEWXF;
  431.       UF=NEWUF;
  432.       MCNT=NEWMCNT;
  433. }
  434.  
  435. /**************************************************/
  436. int class(c)
  437. char c;
  438. {
  439.       if(c==TCVAL) return(TRANSLATE);
  440.       if (c==CFVAL) return(CONTROL);
  441.       if(c>' ') return(BLACK);
  442.       if(c==' ') return(WHITE);
  443.       if(c=='\n') return(SENTINEL);
  444.       if(c=='\r') return(SENTINEL);
  445.       if(c==TAB) return(HTAB);
  446.       if(!c) return(SENTINEL);
  447.       return(OTHERS);
  448. }
  449. /**************************************************/
  450. fraction()      /*put printer in fractional spcing mode;
  451.                         set FRQ*/
  452. {
  453.       if(!FRQ && FRSTRING && (FRVAL!=1))
  454.       {
  455.             outstr(FRSTRING);
  456.             FRQ = TRUE;
  457.       }
  458. }
  459. /**************************************************/
  460. whole()         /*put printer in whole line spacing;
  461.                         reset FRQ */
  462. {
  463.       if(FRQ && WHSTRING)
  464.       {
  465.             outstr(WHSTRING);
  466.             FRQ = FALSE;
  467.       }
  468. }
  469. /**************************************************/
  470. trch(c) /*output string translation of c*/
  471. char c;
  472. {
  473.       char *p;
  474.       if(c<' ') {
  475.             putchar(TCVAL);
  476.             putchar(c);
  477.             return;
  478.       }
  479.       p = TPTR[c-' '];
  480.       if(p) outstr(p);
  481.       else    {/*not initialized*/
  482.             putchar(TCVAL);
  483.             putchar('?');
  484.       }
  485. }
  486. /****************************************/
  487. pcont(c) /*output printer control string for c*/
  488. char c;
  489. {
  490.       char *p;
  491.       if(c<' ') {
  492.             putchar(CFVAL);
  493.             putchar(c);
  494.             return;
  495.       }
  496.       p = CPTR[c-' '];
  497.       if(p) outstr(p);
  498.       else    {/*not initialized*/
  499.             putchar(CFVAL);
  500.             putchar('?');
  501.       }
  502. }
  503.